package com.indsvr.entity.response;

import com.indsvr.entity.xunlei.XunLeiTask;
import com.indsvr.entity.xunlei.XunLeiTaskStatusEnum;
import lombok.Data;

@Data
public class Task {

    private String id;

    private String name;

    private int status;

    private String statusTitle;

    private String icon;

    private String time;

    private String size;

    private long progress;

    private String speed;

    public static Task to(XunLeiTask.Task xunLeiTask){
        Task task = new Task();
        task.id=xunLeiTask.getId();
        task.name= xunLeiTask.getName();
        task.status=xunLeiTask.getPhase().getValue();
        task.statusTitle=xunLeiTask.getPhase().getMsg();
        task.icon=xunLeiTask.getIcon_link();
        task.time=xunLeiTask.getUpdated_time().split("\\.")[0].replace("T", " ");
        task.size=jsSize(xunLeiTask.getFile_size());

        long l = Long.parseLong(xunLeiTask.getFile_size());
        String checkedSize = xunLeiTask.getParams().getChecked_size();

        if (l!=0&&checkedSize!=null) {
            task.progress = 100 * Long.parseLong(checkedSize) / l;
        }

        task.speed = jsSpeed(xunLeiTask.getParams().getSpeed());
        return task;
    }


    public static String jsSize(String file_size) {
        long sizel = Long.parseLong(file_size);
        long l = sizel / 1024;
        if (l<1024) {
            return l+"KB";
        }else if (l<1024*1024){
            return l/1024+"MB";
        }else{
            return l/1024/1024+"GB";
        }
    }

    public static String jsSpeed(String file_size) {
        if (file_size==null) {
            return "0KB/s";
        }
        long sizel = Long.parseLong(file_size);
        long l = sizel / 1024;
        if (l<1024) {
            return l+"KB/s";
        }else if (l<1024*1024){
            return l/1024+"MB/s";
        }else{
            return l/1024/1024+"GB/s";
        }
    }

}
